


<!DOCTYPE html>
<html id="htmlId">
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 
  <title>Coverage Report > CartServiceImpl</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/idea.min.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.min.js"></script>
  <script type="text/javascript" src="../../js/highlightjs-line-numbers.min.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">com.ie.service.before</a>
</div>

<h1>Coverage Summary for Class: CartServiceImpl (com.ie.service.before)</h1>

<table class="coverageStats">

<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Branch, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">CartServiceImpl</td>
<td class="coverageStat">
  <span class="percent">
    11.1%
  </span>
  <span class="absValue">
    (1/9)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/42)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0.9%
  </span>
  <span class="absValue">
    (1/106)
  </span>
</td>
</tr>
  <tr>
    <td class="name">CartServiceImpl$$SpringCGLIB$$0</td>
  </tr>
<tr>
  <td class="name"><strong>Total</strong></td>
<td class="coverageStat">
  <span class="percent">
    11.1%
  </span>
  <span class="absValue">
    (1/9)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/42)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0.9%
  </span>
  <span class="absValue">
    (1/106)
  </span>
</td>
</tr>
</table>

<br/>
<br/>


<pre>
<code class="sourceCode" id="sourceCode">&nbsp;package com.ie.service.before;
&nbsp;
&nbsp;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
&nbsp;import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
&nbsp;import com.baomidou.mybatisplus.core.toolkit.Wrappers;
&nbsp;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
&nbsp;import com.ie.common.http.ResponseResult;
&nbsp;import com.ie.entity.admin.CartEntity;
&nbsp;import com.ie.entity.admin.GoodsEntity;
&nbsp;import com.ie.mapper.before.CartMapper;
&nbsp;import com.ie.service.admin.GoodsService;
&nbsp;import org.springframework.beans.factory.annotation.Autowired;
&nbsp;import org.springframework.stereotype.Service;
&nbsp;import org.springframework.transaction.annotation.Transactional;
&nbsp;
&nbsp;import java.math.BigDecimal;
&nbsp;import java.util.ArrayList;
&nbsp;import java.util.HashMap;
&nbsp;import java.util.List;
&nbsp;import java.util.Map;
&nbsp;
&nbsp;@Service
<b class="fc">&nbsp;public class CartServiceImpl extends ServiceImpl&lt;CartMapper, CartEntity&gt; implements CartService {</b>
&nbsp;    @Autowired
&nbsp;    private GoodsService goodsService; // 注入商品服务，用于查询商品详情
&nbsp;
&nbsp;    @Override
&nbsp;    public ResponseResult&lt;Map&lt;String, Object&gt;&gt; addCart(CartEntity cartEntity) {
<b class="nc">&nbsp;        Long goodsNum = lambdaQuery().eq(CartEntity::getBusertableId, cartEntity.getBusertableId())</b>
<b class="nc">&nbsp;                .eq(CartEntity::getGoodstableId, cartEntity.getGoodstableId()).count();</b>
<b class="nc">&nbsp;        boolean result = false;</b>
<b class="nc">&nbsp;        if (goodsNum &gt; 0) {</b>
&nbsp;            //更新购物车数量
<b class="nc">&nbsp;            LambdaUpdateWrapper&lt;CartEntity&gt; updateWrapper = new LambdaUpdateWrapper&lt;&gt;();</b>
<b class="nc">&nbsp;            updateWrapper.set(CartEntity::getShoppingnum, cartEntity.getShoppingnum() + 1)</b>
<b class="nc">&nbsp;                    .eq(CartEntity::getBusertableId, cartEntity.getBusertableId())</b>
<b class="nc">&nbsp;                    .eq(CartEntity::getGoodstableId, cartEntity.getGoodstableId());</b>
<b class="nc">&nbsp;            result = update(updateWrapper);</b>
&nbsp;        } else {//添加新商品到购物车
<b class="nc">&nbsp;            result = save(cartEntity);</b>
&nbsp;        }
<b class="nc">&nbsp;        if (result) {</b>
<b class="nc">&nbsp;            return ResponseResult.getMessageResult(null, &quot;A001&quot;);</b>
&nbsp;        } else {
<b class="nc">&nbsp;            return ResponseResult.getMessageResult(null, &quot;A002&quot;);</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    @Override
&nbsp;    @Transactional
&nbsp;    public ResponseResult&lt;Map&lt;String, Object&gt;&gt; bupDateCart(CartEntity cartEntity) {
&nbsp;
<b class="nc">&nbsp;        List&lt;Integer&gt; bcid = cartEntity.getBcid();</b>
<b class="nc">&nbsp;        List&lt;Integer&gt; bshoppingnum = cartEntity.getBshoppingnum();</b>
<b class="nc">&nbsp;        ArrayList&lt;CartEntity&gt; cartEntities = new ArrayList&lt;CartEntity&gt;();</b>
<b class="nc">&nbsp;        for (int i = 0; i &lt; bcid.size(); i++) {</b>
<b class="nc">&nbsp;            CartEntity cartEntity1 = new CartEntity();</b>
<b class="nc">&nbsp;            cartEntity1.setId(bcid.get(i));</b>
<b class="nc">&nbsp;            cartEntity1.setShoppingnum(bshoppingnum.get(i));</b>
<b class="nc">&nbsp;            cartEntities.add(cartEntity1);</b>
&nbsp;        }
<b class="nc">&nbsp;        boolean b = updateBatchById(cartEntities);</b>
<b class="nc">&nbsp;        if (b) {</b>
<b class="nc">&nbsp;            return ResponseResult.getMessageResult(null, &quot;A001&quot;);</b>
&nbsp;        } else {
<b class="nc">&nbsp;            return ResponseResult.getMessageResult(null, &quot;A002&quot;);</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    @Override
&nbsp;    public ResponseResult&lt;Map&lt;String, Object&gt;&gt; clearCart(CartEntity cartEntity) {
<b class="nc">&nbsp;        if (cartEntity == null || cartEntity.getBusertableId() == null) {</b>
<b class="nc">&nbsp;            return ResponseResult.error(&quot;参数不能为空&quot;);</b>
&nbsp;        }
&nbsp;
<b class="nc">&nbsp;        QueryWrapper&lt;CartEntity&gt; queryWrapper = new QueryWrapper&lt;&gt;();</b>
<b class="nc">&nbsp;        queryWrapper.eq(&quot;busertable_id&quot;, cartEntity.getBusertableId());</b>
<b class="nc">&nbsp;        boolean result = remove(queryWrapper);</b>
&nbsp;
<b class="nc">&nbsp;        return result ?</b>
<b class="nc">&nbsp;                ResponseResult.getMessageResult(null, &quot;A001&quot;) :</b>
<b class="nc">&nbsp;                ResponseResult.getMessageResult(null, &quot;A002&quot;);</b>
&nbsp;    }
&nbsp;
&nbsp;
&nbsp;    @Override
&nbsp;    public ResponseResult&lt;Map&lt;String, Object&gt;&gt; getCartList(Integer busertableId) {
<b class="nc">&nbsp;        Map&lt;String, Object&gt; resultMap = new HashMap&lt;&gt;();</b>
<b class="nc">&nbsp;        List&lt;Map&lt;String, Object&gt;&gt; cartGoodsList = new ArrayList&lt;&gt;();</b>
&nbsp;
<b class="nc">&nbsp;        if (busertableId == null) {</b>
<b class="nc">&nbsp;            resultMap.put(&quot;cartList&quot;, new ArrayList&lt;&gt;());</b>
<b class="nc">&nbsp;            resultMap.put(&quot;totalPrice&quot;, BigDecimal.ZERO);</b>
<b class="nc">&nbsp;            return ResponseResult.success(resultMap, &quot;用户ID不能为空&quot;);</b>
&nbsp;        }
&nbsp;
<b class="nc">&nbsp;        QueryWrapper&lt;CartEntity&gt; queryWrapper = new QueryWrapper&lt;&gt;();</b>
<b class="nc">&nbsp;        queryWrapper.eq(&quot;busertable_id&quot;, busertableId);</b>
<b class="nc">&nbsp;        List&lt;CartEntity&gt; cartEntities = list(queryWrapper);</b>
&nbsp;
<b class="nc">&nbsp;        if (cartEntities.isEmpty()) {</b>
<b class="nc">&nbsp;            resultMap.put(&quot;cartList&quot;, new ArrayList&lt;&gt;());</b>
<b class="nc">&nbsp;            resultMap.put(&quot;totalPrice&quot;, BigDecimal.ZERO);</b>
<b class="nc">&nbsp;            return ResponseResult.success(resultMap, &quot;购物车数据获取成功&quot;);</b>
&nbsp;        }
&nbsp;
<b class="nc">&nbsp;        for (CartEntity cartEntity : cartEntities) {</b>
<b class="nc">&nbsp;            Map&lt;String, Object&gt; goodsMap = new HashMap&lt;&gt;();</b>
<b class="nc">&nbsp;            GoodsEntity goods = goodsService.getById(cartEntity.getGoodstableId());</b>
<b class="nc">&nbsp;            if (goods != null) {</b>
<b class="nc">&nbsp;                goodsMap.put(&quot;goodsId&quot;, goods.getId());</b>
<b class="nc">&nbsp;                goodsMap.put(&quot;goodsName&quot;, goods.getGname());</b>
<b class="nc">&nbsp;                goodsMap.put(&quot;price&quot;, goods.getGrprice());</b>
<b class="nc">&nbsp;                goodsMap.put(&quot;quantity&quot;, cartEntity.getShoppingnum());</b>
<b class="nc">&nbsp;                goodsMap.put(&quot;imageUrl&quot;, goods.getGpicture());</b>
&nbsp;            }
<b class="nc">&nbsp;            cartGoodsList.add(goodsMap);</b>
&nbsp;        }
&nbsp;
<b class="nc">&nbsp;        BigDecimal totalPrice = cartGoodsList.stream()</b>
<b class="nc">&nbsp;                .filter(item -&gt; item.get(&quot;price&quot;) != null &amp;&amp; item.get(&quot;quantity&quot;) != null)</b>
<b class="nc">&nbsp;                .map(item -&gt; {</b>
<b class="nc">&nbsp;                    Double price = (Double) item.get(&quot;price&quot;);</b>
<b class="nc">&nbsp;                    Integer quantity = (Integer) item.get(&quot;quantity&quot;);</b>
<b class="nc">&nbsp;                    return new BigDecimal(price).multiply(new BigDecimal(quantity));</b>
&nbsp;                })
<b class="nc">&nbsp;                .reduce(BigDecimal.ZERO, BigDecimal::add);</b>
&nbsp;
<b class="nc">&nbsp;        resultMap.put(&quot;cartList&quot;, cartGoodsList);</b>
<b class="nc">&nbsp;        resultMap.put(&quot;totalPrice&quot;, totalPrice);</b>
<b class="nc">&nbsp;        return ResponseResult.success(resultMap, &quot;购物车数据获取成功&quot;);</b>
&nbsp;    }
&nbsp;
&nbsp;    // 在 CartServiceImpl 类中添加这个方法（如果没有的话）
&nbsp;    @Override
&nbsp;    public ResponseResult&lt;Map&lt;String, Object&gt;&gt; removeCart(Integer id) {
<b class="nc">&nbsp;        boolean result = removeById(id);</b>
<b class="nc">&nbsp;        if (result) {</b>
<b class="nc">&nbsp;            return ResponseResult.getMessageResult(null, &quot;A001&quot;); // 删除成功</b>
&nbsp;        } else {
<b class="nc">&nbsp;            return ResponseResult.getMessageResult(null, &quot;A002&quot;); // 删除失败</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    @Override
&nbsp;    public ResponseResult&lt;Map&lt;String, Object&gt;&gt; getCartListByBusertableId(String busertableId) {
<b class="nc">&nbsp;        Map&lt;String, Object&gt; resultMap = new HashMap&lt;&gt;();</b>
<b class="nc">&nbsp;        List&lt;Map&lt;String, Object&gt;&gt; cartGoodsList = new ArrayList&lt;&gt;();</b>
&nbsp;
<b class="nc">&nbsp;        if (busertableId == null || busertableId.isEmpty()) {</b>
<b class="nc">&nbsp;            resultMap.put(&quot;cartList&quot;, new ArrayList&lt;&gt;());</b>
<b class="nc">&nbsp;            resultMap.put(&quot;totalPrice&quot;, BigDecimal.ZERO);</b>
<b class="nc">&nbsp;            return ResponseResult.success(resultMap, &quot;用户ID不能为空&quot;);</b>
&nbsp;        }
&nbsp;
&nbsp;        try {
<b class="nc">&nbsp;            Integer id = Integer.parseInt(busertableId);</b>
<b class="nc">&nbsp;            List&lt;CartEntity&gt; cartEntities = baseMapper.selectByBusertableId(id);</b>
&nbsp;
<b class="nc">&nbsp;            if (cartEntities.isEmpty()) {</b>
<b class="nc">&nbsp;                resultMap.put(&quot;cartList&quot;, new ArrayList&lt;&gt;());</b>
<b class="nc">&nbsp;                resultMap.put(&quot;totalPrice&quot;, BigDecimal.ZERO);</b>
<b class="nc">&nbsp;                return ResponseResult.success(resultMap, &quot;购物车数据获取成功&quot;);</b>
&nbsp;            }
&nbsp;
<b class="nc">&nbsp;            for (CartEntity cartEntity : cartEntities) {</b>
<b class="nc">&nbsp;                Map&lt;String, Object&gt; goodsMap = new HashMap&lt;&gt;();</b>
<b class="nc">&nbsp;                GoodsEntity goods = goodsService.getById(cartEntity.getGoodstableId());</b>
<b class="nc">&nbsp;                if (goods != null) {</b>
<b class="nc">&nbsp;                    goodsMap.put(&quot;goodsId&quot;, goods.getId());</b>
<b class="nc">&nbsp;                    goodsMap.put(&quot;goodsName&quot;, goods.getGname());</b>
<b class="nc">&nbsp;                    goodsMap.put(&quot;price&quot;, goods.getGrprice());</b>
<b class="nc">&nbsp;                    goodsMap.put(&quot;quantity&quot;, cartEntity.getShoppingnum());</b>
<b class="nc">&nbsp;                    goodsMap.put(&quot;imageUrl&quot;, goods.getGpicture());</b>
&nbsp;                }
<b class="nc">&nbsp;                cartGoodsList.add(goodsMap);</b>
&nbsp;            }
&nbsp;
<b class="nc">&nbsp;            BigDecimal totalPrice = cartGoodsList.stream()</b>
<b class="nc">&nbsp;                    .filter(item -&gt; item.get(&quot;price&quot;) != null &amp;&amp; item.get(&quot;quantity&quot;) != null)</b>
<b class="nc">&nbsp;                    .map(item -&gt; {</b>
<b class="nc">&nbsp;                        Double price = (Double) item.get(&quot;price&quot;);</b>
<b class="nc">&nbsp;                        Integer quantity = (Integer) item.get(&quot;quantity&quot;);</b>
<b class="nc">&nbsp;                        return new BigDecimal(price).multiply(new BigDecimal(quantity));</b>
&nbsp;                    })
<b class="nc">&nbsp;                    .reduce(BigDecimal.ZERO, BigDecimal::add);</b>
&nbsp;
<b class="nc">&nbsp;            resultMap.put(&quot;cartList&quot;, cartGoodsList);</b>
<b class="nc">&nbsp;            resultMap.put(&quot;totalPrice&quot;, totalPrice);</b>
<b class="nc">&nbsp;            return ResponseResult.success(resultMap, &quot;购物车数据获取成功&quot;);</b>
&nbsp;        } catch (NumberFormatException e) {
<b class="nc">&nbsp;            resultMap.put(&quot;cartList&quot;, new ArrayList&lt;&gt;());</b>
<b class="nc">&nbsp;            resultMap.put(&quot;totalPrice&quot;, BigDecimal.ZERO);</b>
<b class="nc">&nbsp;            return ResponseResult.success(resultMap, &quot;用户ID必须是数字&quot;);</b>
&nbsp;        }
&nbsp;    }
&nbsp;}
</code>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
      hljs.highlightAll()
      hljs.initLineNumbersOnLoad();
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2025-06-15 18:23</div>
</div>
</body>
</html>
